package com.kaibes.platform.article.util;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kaibes.core.base.util.StringUtils;
import com.kaibes.orm.core.core.Page;
import com.kaibes.platform.article.db.entity.Article;
import com.kaibes.platform.article.db.entity.ArticleTag;
import com.kaibes.platform.article.db.service.ArticleService;
import com.kaibes.platform.article.db.service.ArticleTagService;
import com.kaibes.platform.article.enums.ArticleStatus;
import com.kaibes.web.util.WebUtils;

@Component
public class ArticleUtils {

//    private final static String IMAGE_PATTERN = "https{0,1}://[a-zA-Z0-9:]+/[a-zA-Z0-9/]+\\.((jpg)|(jpeg)|(gif)|(png))";
    private final static String IMAGE_PATTERN = "!\\[[^\\]]+\\]\\(([^\\)]+)\\)";
    
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleTagService articleTagService;
    
    public long count() {
        return articleService.count();
    }
    
    public long countToday() {
        return articleService.countToday();
    }
    
    public long countYesterday() {
        return articleService.countYesterday();
    }
    
    public long countByCategoryId(short categoryId) {
        return articleService.countByCategoryId(categoryId);
    }
    
    public long countByUserId(Integer userId) {
        return articleService.countByUserId(userId);
    }
    
    public long commentCountByCategoryId(short categoryId) {
        return articleService.getCommentCountByCategoryId(categoryId);
    }
    
    public String[] splitTags(String tags) {
        return tags.split(";");
    }
    
    public String getTitle(BigInteger id) {
        return articleService.getTitleById(id);
    }
    
    public Article getTitleId(BigInteger id) {
        return articleService.getTitleId(id);
    }
    
    public List<Article> listTitleIdThumbnailHot(int count) {
        return articleService.listTitleIdThumbnailContentHot(count);
    }
    
    public List<Article> listTitleIdNew(int count) {
        return articleService.listTitleIdNew(count);
    }
    
    public List<Article> listTitleIdHot(int count) {
        return articleService.listTitleIdHot(count);
    }
    
    public List<Article> listNew(int count) {
        return articleService.list(count);
    }
    
    public List<Article> listHot(int count) {
        return articleService.listByOrderViewCount(count);
    }
    
    public List<Article> listReply(int count) {
        return articleService.listByOrderCommentTime(count);
    }
    
    public List<Article> listTops(int count) {
        return articleService.listTops(count);
    }
    
    public List<ArticleTag> listAllArticleTag() {
        return articleTagService.list();
    }
    
    public String getThumbnail(Article article) {
        String thumbnail = article.getThumbnail();
        if (StringUtils.isBlank(article.getThumbnail()) && article.getContent() != null) {
            thumbnail = StringUtils.findStringG1(article.getContent(), IMAGE_PATTERN);
            article.setThumbnail(thumbnail);
        }
        return thumbnail;
    }
    
    public Page<Article> page(int size) {
        HttpServletRequest request = WebUtils.getRequest();
        int page = (int) request.getAttribute("page");
        String tag = (String) request.getAttribute("tag");
        Short categoryId = (Short) request.getAttribute("categoryId");
        String search = (String) request.getAttribute("search");
        Page<Article> articlePage = null;
        if (categoryId != null) {
            articlePage = articleService.pageAscByStatusAndCategoryId(page, size, ArticleStatus.PUBLISH, categoryId);
        } else if (tag != null) {
            articlePage = articleService.pageAscByStatusAndTag(page, size, ArticleStatus.PUBLISH, tag);
        } else if (search != null) {
            articlePage = articleService.pageAscByStatusAndSearch(page, size, ArticleStatus.PUBLISH, search);
        } else {
            articlePage = articleService.pageAscByStatus(page, size, ArticleStatus.PUBLISH);
        }
        return articlePage;
    }
    
    public BigInteger last(Integer id) {
        return articleService.lastIdById(id);
    }
    
    public BigInteger next(Integer id) {
        return articleService.nextIdById(id);
    }
    
}
